<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商品id" prop="spId">
        <el-input
          v-model="queryParams.spId"
          placeholder="请输入商品id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="最低价格" prop="beginspJiage">
        <el-input
          v-model="beginSpJiage"
          placeholder="请输入开始价格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="最高价格" prop="endspJiage">
        <el-input
          v-model="endSpJiage"
          placeholder="请输入结束价格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="最低月售" prop="beginSpYueshou">
        <el-input
          v-model="beginSpYueshou"
          placeholder="请输入最低月售"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="最高月售" prop="endSpYueshou">
        <el-input
          v-model="endSpYueshou"
          placeholder="请输入最高月售"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="店铺名称" prop="dpName">
        <el-input
          v-model="dpName"
          placeholder="请输入店铺名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="daterangeSpAddtime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="审核状态" prop="spZhuangtai">
<!--        <el-input-->
<!--          v-model="queryParams.spZhuangtai"-->
<!--          placeholder="请输入审核状态"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
        <el-select v-model="queryParams.spZhuangtai" placeholder="请选择下拉选择" clearable :style="{width: '100%'}">
          <el-option v-for="(item, index) in shenhelist" :key="index" :label="item.spshName" :value="item.spshId" :disabled="item.disabled"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="商品名称" prop="spName">
        <el-input
          v-model="queryParams.spName"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品分类" prop="spFlId">
<!--        <el-input-->
<!--          v-model="queryParams.spFlId"-->
<!--          placeholder="请输入商品分类"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
        <el-select v-model="queryParams.spFlId" placeholder="请选择下拉选择" clearable :style="{width: '100%'}">
          <el-option v-for="(item, index) in fllist" :key="index" :label="item.flName"
                     :value="item.flId" :disabled="item.disabled"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['shangpin:shangpin:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['shangpin:shangpin:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['shangpin:shangpin:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['shangpin:shangpin:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="shangpinList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="商品id" align="center" prop="spId" />
<!--      <el-table-column label="商品图片" align="center" prop="spTupian" />-->
      <el-table-column label="商品图片" align="center">
<!--        <template slot-scope="scope">-->
<!--          <img :src="scope.row.spTupian" alt="商品图片" style="width: 100px; height: 100px;">-->
<!--        </template>-->
        <template slot-scope="scope">
          <image-preview :src="scope.row.spTupian" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="价格" align="center" prop="spJiage" />
      <el-table-column label="打包费" align="center" prop="spDabaoo" />
      <el-table-column label="库存" align="center" prop="spKucun" />
      <el-table-column label="月售" align="center" prop="spYueshou" />
      <el-table-column label="店铺名称" align="center" prop="dianpu.dpName" />
      <el-table-column label="创建时间" align="center" prop="spAddtime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.spAddtime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="spshenhe.spshName" />
      <el-table-column label="操作" align="center" prop="spCaozuo" />
      <el-table-column label="商品名称" align="center" prop="spName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['shangpin:shangpin:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['shangpin:shangpin:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商品列表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="商品图片" prop="spTupian">
<!--          <el-input v-model="form.spTupian" placeholder="请输入商品图片" />-->
          <image-upload v-model="form.spTupian"/>
        </el-form-item>
        <el-form-item label="价格" prop="spJiage">
          <el-input v-model="form.spJiage" placeholder="请输入价格" />
        </el-form-item>
<!--        ++++++++++++++++++++++++++++++-->
        <el-form-item label="打包费" prop="spDabaoo">
          <el-input v-model="form.spDabaoo" placeholder="请输入价格" />
        </el-form-item>
        <el-form-item label="库存" prop="spKucun">
          <el-input v-model="form.spKucun" placeholder="请输入价格" />
        </el-form-item>
        <el-form-item label="月售" prop="spYueshou">
          <el-input v-model="form.spYueshou" placeholder="请输入价格" />
        </el-form-item>
        <el-form-item label="创建时间" prop="spAddtime">
          <el-date-picker v-model="form.spAddtime"
                          format="yyyy-MM-dd"
                          value-format="yyyy-MM-dd"
                          :style="{width: '100%'}"
                          placeholder="请选择日期选择"
                          clearable>
          </el-date-picker>
        </el-form-item>
<!--        +++++++++++++++++++++++++++++++++-->
        <el-form-item label="店铺名称" prop="spDpid">
<!--          <el-input v-model="form.spDpid" placeholder="请输入店铺id" />-->
          <el-select v-model="form.spDpid" placeholder="请选择下拉选择" clearable :style="{width: '100%'}">
            <el-option v-for="(item, index) in dpList" :key="index" :label="item.dpName"
                       :value="item.dpId" :disabled="item.disabled"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品编号" prop="spBianhao">
          <el-input v-model="form.spBianhao" placeholder="请输入商品编号" />
        </el-form-item>
        <el-form-item label="售卖的状态" prop="spIsshoumai">
<!--          <el-input v-model="form.spIsshoumai" placeholder="请输入售卖的状态" />-->
          <el-select v-model="form.spIsshoumai" placeholder="请选择下拉选择" clearable :style="{width: '100%'}">
            <el-option v-for="(item, index) in shoumaiList" :key="index" :label="item.shoumaiName"
                       :value="item.shoumaiId" :disabled="item.disabled"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品名称" prop="spName">
          <el-input v-model="form.spName" placeholder="请输入商品名称" />
        </el-form-item>
        <el-form-item label="商品分类" prop="spFlId">
<!--          <el-input v-model="form.spFlId" placeholder="请输入商品分类" />-->
          <el-select v-model="form.spFlId" placeholder="请选择下拉选择" clearable :style="{width: '100%'}">
            <el-option v-for="(item, index) in fllist" :key="index" :label="item.flName"
                       :value="item.flId" :disabled="item.disabled"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否是套餐(0是，1不是)" prop="spIstaocan">
<!--          <el-input v-model="form.spIstaocan" placeholder="请输入是否是套餐(0是，1不是)" />-->
          <el-select v-model="form.spIstaocan" placeholder="请选择下拉选择" clearable :style="{width: '100%'}">
            <el-option v-for="(item, index) in taocanList" :key="index" :label="item.taocanName"
                       :value="item.taocanId" :disabled="item.disabled"></el-option>
          </el-select>
        </el-form-item>
        <el-divider content-position="center">商品分类信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddFenlei">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteFenlei">删除</el-button>
          </el-col>
        </el-row>


        <el-table :data="fenleiList" :row-class-name="rowFenleiIndex" @selection-change="handleFenleiSelectionChange" ref="fenlei">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="分类名称" prop="flName" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.flName" placeholder="请输入分类名称" />
            </template>
          </el-table-column>
          <el-table-column label="所属店铺" prop="flDpId" width="150">
            <template slot-scope="scope">
<!--              <el-input v-model="scope.row.flDpId" placeholder="请输入所属店铺的ID" />-->
              <el-select v-model="scope.row.flDpId" placeholder="请选择下拉选择" clearable :style="{width: '100%'}">
                <el-option v-for="(item, index) in dpList" :key="index" :label="item.dpName"
                           :value="item.dpId" :disabled="item.disabled"></el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="创建时间" prop="flAddtime" width="240">
            <template slot-scope="scope">
              <el-date-picker clearable v-model="scope.row.flAddtime" type="date" value-format="yyyy-MM-dd" placeholder="请选择创建时间" />
            </template>
          </el-table-column>
          <el-table-column label="是否是可用的分类" prop="flIsuser" width="150">
            <template slot-scope="scope">
<!--              <el-input v-model="scope.row.flIsuser" placeholder="请输入是否是可用的分类" />-->
              <el-select v-model="scope.row.flIsuser" placeholder="请选择下拉选择" clearable :style="{width: '100%'}">
                <el-option v-for="(item, index) in keyongList" :key="index" :label="item.keyongName"
                           :value="item.keyongId" :disabled="item.disabled"></el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="审核该分类的状态" prop="flZhuangtai" width="150">
            <template slot-scope="scope">
<!--              <el-input v-model="scope.row.flZhuangtai" placeholder="请输入审核该分类的状态" />-->
              <el-select v-model="scope.row.flZhuangtai" placeholder="请选择下拉选择" clearable :style="{width: '100%'}">
                <el-option v-for="(item, index) in shenhelist" :key="index" :label="item.spshName" :value="item.spshId" :disabled="item.disabled"></el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="操作" prop="flCaozuo" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.flCaozuo" placeholder="请输入操作" />
            </template>
          </el-table-column>
          <el-table-column label="审核原因" prop="flYuanyin" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.flYuanyin" placeholder="请输入审核原因" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listShangpin,
  getShangpin,
  delShangpin,
  addShangpin,
  updateShangpin,
  listSpshenhe, listDianpu
} from "@/api/shangpin/shangpin";
import {listFenlei} from "@/api/system/fenlei";

export default {
  name: "Shangpin",
  data() {
    return {
      //下拉分类
      fllist: [{
        "flName": "选项一",
        "flId": 1
      }, {
        "flName": "选项二",
        "flId": 2
      }],
      // 下拉审核
      shenhelist:[{
        "spshName": "选项一",
        "spshId": 1
      }, {
        "spshName": "选项二",
        "spshId": 2
      }],
      //是否是套餐
      taocanList:[{
        "taocanName": "是套餐",
        "taocanId": 0
      }, {
        "taocanName": "不是套餐",
        "taocanId": 1
      }],
      //店铺选择
      dpList:[{
        "dpName": "是套餐",
        "dpId": 0
      }, {
        "dpName": "不是套餐",
        "dpId": 1
      }],
      //售卖的状态
      shoumaiList:[{
        "shoumaiName": "售卖中",
        "shoumaiId": 0
      }, {
        "shoumaiName": "已下架",
        "shoumaiId": 1
      }],
      //是否可用下拉设置
      keyongList:[{
        "keyongName": "可用",
        "keyongId": 0
      }, {
        "keyongName": "不可用",
        "keyongId": 1
      }],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedFenlei: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品列表表格数据
      shangpinList: [],
      // 商品分类表格数据
      fenleiList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 审核拒绝的原因描述时间范围
      daterangeSpAddtime: [],
      // 价格范围
      beginSpJiage:null,
      endSpJiage:null,
      //月售的范围
      beginSpYueshou:null,
      endSpYueshou:null,
      //店铺名称
      dpName:null,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        spId: null,
        spJiage: null,
        // beginSpJiage:null,
        // endSpJiage:null,
        spYueshou: null,
        spDpid: null,
        spAddtime: null,
        spZhuangtai: null,
        spName: null,
        spFlId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        spDpid: [
          { required: true, message: "店铺id不能为空", trigger: "blur" }
        ],
        spZhuangtai: [
          { required: true, message: "审核状态不能为空", trigger: "blur" }
        ],
        spIsshoumai: [
          { required: true, message: "售卖的状态不能为空", trigger: "blur" }
        ],
        spName: [
          { required: true, message: "商品名称不能为空", trigger: "blur" }
        ],
        spIstaocan: [
          { required: true, message: "是否是套餐(0是，1不是)不能为空", trigger: "blur" }
        ],
        spFlId: [
          { required: true, message: "商品分类不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getFlList();
    this.getShenHeList();
    this.getDpList();
  },
  methods: {
    /** 查询商品列表列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      // if (this.queryParams.beginSpJiage === null || this.queryParams.endSpJiage === null) {
      //   alert('请输入有效的价格范围！');
      //   return;
      // }
      if (null != this.daterangeSpAddtime && '' != this.daterangeSpAddtime) {
        this.queryParams.params["beginSpAddtime"] = this.daterangeSpAddtime[0];
        this.queryParams.params["endSpAddtime"] = this.daterangeSpAddtime[1];
      }
      if (null != this.endSpJiage && '' != this.endSpJiage && null != this.beginSpJiage && '' != this.beginSpJiage ) {
        this.queryParams.params["beginSpJiage"] = this.beginSpJiage;
        this.queryParams.params["endSpJiage"] = this.endSpJiage;
      }
      if (null != this.endSpYueshou && '' != this.endSpYueshou && null != this.beginSpYueshou && '' != this.beginSpYueshou ) {
        this.queryParams.params["beginSpYueshou"] = this.beginSpYueshou;
        this.queryParams.params["endSpYueshou"] = this.endSpYueshou;
      }
      if (null != this.dpName && '' != this.dpName) {
        this.queryParams.params["dpName"] = this.dpName;
      }

      listShangpin(this.queryParams).then(response => {
        this.shangpinList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 分类下拉框
    getFlList(){
      listFenlei(null).then(response =>{
        this.fllist = response.rows;
      })
    },
    //审核下拉框
    getShenHeList(){
      listSpshenhe(null).then(response =>{
        this.shenhelist = response.rows;
      })
    },
    //店铺下拉框
    getDpList(){
      listDianpu(null).then(response =>{
        this.dpList = response.rows;
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        spId: null,
        spTupian: null,
        spJiage: null,
        spDabaoo: null,
        spKucun: null,
        spYueshou: null,
        spDpid: null,
        spAddtime: null,
        spKaishitime: null,
        spJieshutime: null,
        spZhuangtai: null,
        spCaozuo: null,
        spBianhao: null,
        spIsshoumai: null,
        spName: null,
        spFlId: null,
        spIstaocan: null,
        spPianyi: null
      };
      this.fenleiList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeSpAddtime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.spId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商品列表";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const spId = row.spId || this.ids
      getShangpin(spId).then(response => {
        this.form = response.data;
        this.fenleiList = response.data.fenleiList;
        this.open = true;
        this.title = "修改商品列表";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.fenleiList = this.fenleiList;
          if (this.form.spId != null) {
            updateShangpin(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addShangpin(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const spIds = row.spId || this.ids;
      this.$modal.confirm('是否确认删除商品列表编号为"' + spIds + '"的数据项？').then(function() {
        return delShangpin(spIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 商品分类序号 */
    rowFenleiIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 商品分类添加按钮操作 */
    handleAddFenlei() {
      let obj = {};
      obj.flName = "";
      obj.flMiaoshu = "";
      obj.flDpId = "";
      obj.flAddtime = "";
      obj.flIsuser = "";
      obj.flZhuangtai = "";
      obj.flCaozuo = "";
      obj.flYuanyin = "";
      obj.flJujue = "";
      this.fenleiList.push(obj);
    },
    /** 商品分类删除按钮操作 */
    handleDeleteFenlei() {
      if (this.checkedFenlei.length == 0) {
        this.$modal.msgError("请先选择要删除的商品分类数据");
      } else {
        const fenleiList = this.fenleiList;
        const checkedFenlei = this.checkedFenlei;
        this.fenleiList = fenleiList.filter(function(item) {
          return checkedFenlei.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleFenleiSelectionChange(selection) {
      this.checkedFenlei = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('shangpin/shangpin/export', {
        ...this.queryParams
      }, `shangpin_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
